package com.maoye.ossp.sys.base.controller;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SysCodeRule;
import com.maoye.ossp.sys.base.entity.SysCodeRuleEntry;
import com.maoye.ossp.sys.base.service.SysCodeRuleEntryService;
import com.maoye.ossp.sys.base.service.SysCodeRuleService;

@Controller
@RequestMapping(value = "sys/syscoderule/")
public class SysCodeRuleController extends BaseController {

	@Resource
	SysCodeRuleService service;
	@Resource
	SysCodeRuleEntryService  entryService;
	
	
	@RequestMapping(value = "list")
	public String list(SysCodeRule sysCodeRule, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<SysCodeRule> list =service.getAll();
		model.addAttribute("list", list);
		return "/sys/coderule/list";
	}
	
	
	@RequestMapping(value = "add")
	public String add(SysCodeRule sysCodeRule, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		return "/sys/coderule/add";
	}
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "save")
	public String save(SysCodeRule sysCodeRule, HttpServletRequest request, HttpServletResponse response, Model model,String sysCodeRuleEntry,String deleteEntry) {
		if(sysCodeRule.getId()==null){
			sysCodeRule.setId(idGenerator.getUId());
			service.create(sysCodeRule);
		}else{
			service.update(sysCodeRule);
		}
		Gson gson = new Gson();
		
		if(!StringUtils.isEmpty(sysCodeRuleEntry)){
			@SuppressWarnings("serial")
			List<SysCodeRuleEntry> list=gson.fromJson(sysCodeRuleEntry, new TypeToken<List<SysCodeRuleEntry>>(){}.getType()); 
			for (SysCodeRuleEntry codleEntry : list) {
				if(codleEntry.getId()!=null&&codleEntry.getId()!=0){
					entryService.update(codleEntry);
				}else{
					codleEntry.setId(idGenerator.getUId());
					codleEntry.setSyscoderuleid(sysCodeRule.getId());
					entryService.create(codleEntry);
				}
			}
		
		}
		if(!StringUtils.isEmpty(deleteEntry)){
			String [] delId=deleteEntry.split(",");
			for (int i = 0; i < delId.length; i++) {
				entryService.remove(Long.valueOf(delId[i]));
			}
		}
		return "redirect:/sys/syscoderule/list";
	}
	
	
	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable Long id, Model model,
			RedirectAttributes redirectAttributes) {
		service.remove(id);
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/sys/syscoderule/list";
	}
	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable Long id, Model model,SysCodeRule sysCodeRule) {
		SysCodeRule codeRule = service.get(id);
		HashMap<String,String> map =Maps.newHashMap();
		map.put("syscoderuleid", id.toString());
		List<SysCodeRuleEntry> list = entryService.getcodeRuleEntryById(id.toString());
		model.addAttribute("list", list);
		model.addAttribute("sysCodeRule", codeRule);
		return "/sys/coderule/add"	;
	}
}
